/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.zhaw.ads.services;

import ch.zhaw.ads.CommandExecuter;
import ch.zhaw.ads.list.BananaStackException;
import ch.zhaw.ads.list.BananaStackI;
import ch.zhaw.ads.list.BananaStackImpl;

/**
 *
 * @author framet
 */
public class BananaStackService implements CommandExecuter {

    private BananaStackI bananaStack;

    public BananaStackService() {
        bananaStack = new BananaStackImpl();
    }

    @Override
    public String execute(String command) throws Exception {
        switch (command.toLowerCase()) {
            case "empty":
                return commandEmpty();
            case "pop":
                return commandPop();
            case "top":
                return commandTop();
            default:
                return commandPush(command);
        }
    }

    private String commandEmpty() {
        bananaStack = new BananaStackImpl();
        return "Msg:201 - New stack created.";
    }

    private String commandPop() {
        try {
            return String.valueOf(bananaStack.pop());
        } catch (BananaStackException ex) {
            return ex.getMessage();
        }
    }

    private String commandTop() {
        try {
            return String.valueOf(bananaStack.top());
        } catch (BananaStackException ex) {
            return ex.getMessage();
        }
    }

    private String commandPush(String command) {
        try {
            bananaStack.push(Integer.valueOf(command));
        } catch (NumberFormatException ex) {
            return "Error:100 - The BananaStack only allows Numbers (int).";
        }
        return "Msg:200 - Element added to storage.";
    }
}
